import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.bind.JAXBException;

public class TextHandling {
	public static String name = "test";
	public static String nameFormatted = "test";
	private final static String newline = "\n";
	public static String notes = "c.d.ef.g.a.b";
	public static boolean keySet = false;
	public static boolean tempoSet = false;
	public static boolean newLearn = true;
	public static boolean searchComplete = false;
	
	public static String artist = "null";
	public static String song = "null";
	public static String score = "null";
	public static String style = "null";
	
	public static void receive(String text) throws JAXBException, IOException {

		text = text.trim();
		text = text.toLowerCase();
		int textLength = text.length();



//		------------ SCORE / INSTRUCTIONS ---------------------------------------------------
		
		if (textLength > 9) {
			if (text.substring(0, 10).equals("add artist")) {
				nameFormatted = text.toLowerCase().substring(11).replace(' ',
						'_');
				nameFormatted = nameFormatted.concat("_artist");
				String[] temp = Main.stats.artists;
				Main.stats.artists = new String[Main.stats.artists.length+1];
				System.arraycopy(temp, 0, Main.stats.artists, 0, temp.length);
				Main.textGUI.textArea.setText("adding artist "
						+ Main.stats.artists[Main.stats.artists.length-1] + "\n");
				Main.stats.artists[Main.stats.artists.length-1]=nameFormatted;
				Main.score.load[0]=Main.stats.artists;
				Main.textGUI.textArea.setText("Artists: ");
				for (int i=0;i<Main.stats.artists.length;i++) {
					Main.textGUI.textArea.append("\n"+Main.stats.artists[i]);
				}
			}
		}

		if (textLength > 7) {
			if (text.substring(0, 8).equals("set song")) {
				nameFormatted = text.toLowerCase().substring(9).replace(' ',
						'_');
				nameFormatted = nameFormatted.concat("_song");
				Main.score.load[1] = new String[1];
				Main.score.load[1][0] = nameFormatted;
				Main.textGUI.textArea.setText("setting song "
						+ Main.score.load[1][0] + "\n");
			}
		}

		if (textLength > 17) {
			if (text.substring(0, 18).equals("remove instruction")) {
				String[] instStringArray = text.substring(19).split(" ");
				int[] instArray = new int[instStringArray.length];
				int removeRow =	instArray[0] = Integer.parseInt(instStringArray[0]);
				if (removeRow >= Main.score.instructions.length)
				{
					removeRow = Main.score.instructions.length-1;
					System.out.println("*** Remove row out of bounds, adjusted to last row "+(Main.score.instructions.length-1));
				}
				int[][] tempInstructions = new int[Main.score.instructions.length-1][];
				int index = 0;
				while (index < removeRow)
				{
					tempInstructions[index]=Main.score.instructions[index];
					index++;
				}
				index++;
				while (index<Main.score.instructions.length)
				{
					tempInstructions[index-1]=Main.score.instructions[index];
					index++;
				}
				Main.score.instructions=tempInstructions;
				printInstructions();
			}
		}
		

		if (textLength > 15) {
			if (text.substring(0, 16).equals("new instructions")) {
				Main.score.instructions = null;
			}
		}
		
		if (textLength > 14) {
			if (text.substring(0, 15).equals("add instruction")) {
				String[] instStringArray = text.substring(16).split(" ");
				int[] instArray = new int[instStringArray.length];
				for (int i = 0; i < instStringArray.length; i++) {
					instArray[i] = Integer.parseInt(instStringArray[i]);
				}
				if (instArray.length == 3) {
					// iterate to add instruction in correct row of instruction
					// set
					if (Main.score.instructions == null) {
						Main.score.instructions = new int[1][3];
						Main.score.instructions[0] = instArray;
					}
					else {
						int index=Main.score.instructions.length-1;
						boolean bound = false;
						while (!bound&&index>=0)
						{
							if  (instArray[0]>Main.score.instructions[index][0]) {
								bound = true;
							}
							else if (instArray[0]==Main.score.instructions[index][0]&&instArray[1]>Main.score.instructions[index][1]) {
								bound = true;
							}
							else {
								index--;
							}
						}
						int[][] tempInstructions = new int[Main.score.instructions.length+1][];
						int index2=0;
						while (index2<=index)
						{
							tempInstructions[index2]=Main.score.instructions[index2];
							index2++;
						}
						tempInstructions[index2]=instArray;
						while (index2<tempInstructions.length-1)
						{
							tempInstructions[index2+1]=Main.score.instructions[index2];
							index2++;
						}
						Main.score.instructions=tempInstructions;
					}
					Main.textGUI.textArea.setText("adding instruction "
							+ instArray[0] + " " + instArray[1] + " "
							+ instArray[2] + "\n");
					printInstructions();
				} else {
					Main.textGUI.textArea
							.setText("instruction of wrong size! length = "
									+ instArray.length);
				}
			}
		}


		
//		------------ PRINTING ---------------------------------------------------

		if (textLength > 11) {
			if (text.substring(0, 12).equals("print output")) {
				for (int i=0;i<Main.pchOut.length;i++) {
					System.out.print("\nBar: "+i+" Chord: "+Main.chdOut[i]+"\nPitch Array: ");
					for (int j=0;j<Main.pchOut[i].length;j++) {
						System.out.print("\t"+Main.pchOut[i][j]);
					}
					System.out.print("\nOnset Array: ");
					for (int j=0;j<Main.rhyOut[i].length;j++) {
						System.out.print("\t"+Main.rhyOut[i][j]);
					}
				}
			}
		}
		
		if (textLength > 10) {
			if (text.substring(0, 11).equals("print input")) {
				System.out.println("Main.chdIn[0].length = "+Main.chdIn[0].length);
				for (int i=0;i<Main.pchIn.length;i++) {
					System.out.print("\nBar: "+i+" Chord: "+Main.chdIn[0][i]+"\nPitch Array: ");
					for (int j=0;j<Main.pchIn[i].length;j++) {
						System.out.print("\t"+Main.pchIn[i][j]);
					}
					System.out.print("\nOnset Array: ");
					for (int j=0;j<Main.rhyIn[i].length;j++) {
						System.out.print("\t"+Main.rhyIn[i][j]);
					}
				}
			}
		}

		if (textLength > 10) {
			if (text.substring(0, 11).equals("print score")) {
				Main.textGUI.textArea.setText("Artist: ");
				for (int i=0;i<Main.stats.artists.length;i++) {
					Main.textGUI.textArea.append("\n"+Main.stats.artists[i]);
				}
				if (Main.score.load[1].length>0) {
					Main.textGUI.textArea.append("\n\nSong:\n" + Main.score.load[1][0]);
					if (Main.score.instructions != null) {
						Main.textGUI.textArea.append("\n\nInstructions: ");
						for (int i = 0; i < Main.score.instructions.length; i++) {
							Main.textGUI.textArea.append("\n"+Main.score.instructions[i][0] + " "
									+ Main.score.instructions[i][1] + " "
									+ Main.score.instructions[i][2]);
						}
					}
				}
			}
		}
		
		if (textLength > 15) {
			if (text.substring(0, 16).equals("printmatrix song")) {
				try {
					BIABReader.printMatrices();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		if (textLength > 13) {
			if (text.substring(0, 14).equals("printsongstats")) {
				System.out.print("\npublic int[] chordRoots = \t{");
				for (int i = 0; i < Main.stats.chordRoots.length; i++) {
					System.out.print("\t"+Main.stats.chordRoots[i]+", ");
				}
				System.out.print("};\npublic int[] chordQualities = {");
				for (int i = 0; i < Main.stats.chordQualities.length; i++) {
					System.out.print("\t" + Main.stats.chordQualities[i]+", ");
				}
				System.out.print("};\npublic int[] chordDurations = {");
				for (int i = 0; i < Main.stats.chordDurations.length; i++) {
					System.out.print("\t" + Main.stats.chordDurations[i]+",");
				}
				System.out.print("};\n");
				searchComplete=true;
			}
		}
		if (textLength > 8&&!searchComplete) {
			if (text.substring(0, 9).equals("printsong")) {
				System.out.print("\npublic int[] chordRoots = \t{");
				for (int i = 0; i < Main.score.changes[0].length; i++) {
					System.out.print("\t"+Main.score.changes[0][i]+", ");
				}
				System.out.print("};\npublic int[] chordQualities = {");
				for (int i = 0; i < Main.score.changes[1].length; i++) {
					System.out.print("\t" + Main.score.changes[1][i]+", ");
				}
				System.out.print("};\npublic int[] chordDurations = {");
				for (int i = 0; i < Main.score.changes[2].length; i++) {
					System.out.print("\t" + Main.score.changes[2][i]+",");
				}
				System.out.print("\t"+4*48+", ");
				System.out.print("};\n");
			}
		}
		if (textLength > 15) {
			if (text.substring(0, 16).equals("printsongchanges")) {
				System.out.print("\n{\n\t{");
				for (int i = 0; i < Main.score.changes[0].length; i++) {
					System.out.print("\t" + Main.score.changes[0][i]+",");
				}
				System.out.print("\t}\n\t{");
				for (int i = 0; i < Main.score.changes[1].length; i++) {
					System.out.print("\t" + Main.score.changes[1][i]+",");
				}
				System.out.print("\t}\n\t{");
				for (int i = 0; i < Main.score.changes[2].length; i++) {
					System.out.print("\t" + Main.score.changes[2][i]+",");
				}
				System.out.print("\t}\n}");
			}
		}
		if (textLength > 14) {
			if (text.substring(0, 15).equals("printartistsong")) {
				System.out.println("");
				for (int i = 0; i < Main.score.changes[0].length; i++) {
					System.out.print("\t" + Main.stats.chordRoots[i]);
				}
				System.out.println("");
				for (int i = 0; i < Main.score.changes[1].length; i++) {
					System.out.print("\t" + Main.stats.chordQualities[i]);
				}
				System.out.println("");
				for (int i = 0; i < Main.score.changes[2].length; i++) {
					System.out.print("\t" + Main.stats.chordDurations[i]);
				}
			}
		}

		if (textLength > 3) {
			if (text.substring(0, 4).equals("stop")) {
				Main.stop = true;
				Main.score.chordPointer = -1;
			}
		}
		
//		------------ SET ---------------------------------------------------
		
		if (textLength > 11) {
			if (text.substring(0, 12).equals("set songname")) {		
				if (textLength>13)
				{
					nameFormatted = text.toLowerCase().substring(13).replace(' ',
							'_');				
					song = nameFormatted;
				}
			}
		}
		
		if (textLength > 6) {
			if (text.substring(0, 7).equals("set key")) {
				if (textLength > 9) {
					System.out.println(Integer.parseInt(text.substring(8, 10)));
					Main.score.key = Integer.parseInt(text.substring(8, 10));
					Main.stats.key = Main.score.key;
				} else {
					System.out.println(Integer.parseInt(text.substring(8, 9)));
					Main.score.key = Integer.parseInt(text.substring(8, 9));
					Main.stats.key = Main.score.key;
				}
			}
		}
		
		if (textLength > 10) {
			if (text.substring(0, 11).toLowerCase().equals("set isminor")) {
				if (textLength > 12) {
					String[] instStringArray = text.substring(12).split(" ");
					System.out.println(instStringArray[0]);
					if (instStringArray[0].equals("true")) {
						Main.stats.isMinor=true;
					}
					else {
						Main.stats.isMinor=false;
					}

				} 
			}
		}
		
		if (textLength > 8) {
			if (text.substring(0, 9).toLowerCase().equals("set meter")) {
				if (textLength > 10) {
					String[] instStringArray = text.substring(10).split(" ");
					Main.stats.meter=Integer.parseInt(instStringArray[0]);
					System.out.println(instStringArray[0]);

				} 
			}
		}

		if (textLength > 8) {
			if (text.substring(0, 9).equals("set tempo")) {
				if (textLength > 12) {
					System.out
							.println(Integer.parseInt(text.substring(10, 13)));
					Main.bpm = Integer.parseInt(text.substring(10, 13));
				} else {
					System.out
							.println(Integer.parseInt(text.substring(10, 12)));
					Main.bpm = Integer.parseInt(text.substring(10, 12));
				}
			}
		}
		
//		------------ PLAYBACK CONTROLS ---------------------------------------------------
		
		if (textLength>4)
		{
			if (text.substring(0, 5).equals("reset")) {
	
				Main.score.chordPointer = -1;
				Main.score.barPointer = -1;
			}	
		}
		
		if (text.substring(0, 4).equals("play")) {

			Main.stop = false;
			Play play = new Play();
			Thread playbackThread = new Thread(play);
			playbackThread.setPriority(Thread.MAX_PRIORITY);
			playbackThread.start();
		}

		if (text.substring(0, 4).equals("stop")) {
			Main.stop = true;
			Main.score.chordPointer = -1;
			Main.score.barPointer = -1;
		}

		if (textLength > 7) {
			if (text.substring(0, 8).equals("read key")) {
				System.out.println(Main.stats.key);
			}
		}
		
		if (textLength > 9) {
			if (text.substring(0, 10).equals("song")) {
				nameFormatted = text.toLowerCase().substring(11)
						.replace(' ', '_');
				name = text.substring(6);
				Main.xml.newSong(nameFormatted);
				boolean exists = Main.xml.readSongStyle(nameFormatted);
				if (exists) {
					Main.score.key = XML.getSongKey(nameFormatted);
					Main.score.isMinor = XML
							.getSongIsMinor(nameFormatted);
					Main.score.changes[0] = XML
							.getSongChordRoots(nameFormatted);
					Main.score.changes[1] = XML
							.getSongChordQualities(nameFormatted);
					Main.score.changes[2] = XML
							.getSongOnsets(nameFormatted);
					Main.score.pitch = XML
							.getSongMelodyPitch(nameFormatted);
					Main.score.onset = XML
							.getSongMelodyOnset(nameFormatted);
					Main.textGUI.textArea.setText("I remember " + name
							+ " ...I have already learned this song"
							+ newline);
					newLearn = false;
				}
			}
		}
		
//		------------ SAVING ---------------------------------------------------
		
		if (textLength > 9) {
			if (text.substring(0, 10).equals("save score")) {
				if (textLength>11)
				{
					nameFormatted = text.toLowerCase().substring(11).replace(' ',
							'_');
					nameFormatted = nameFormatted.concat("_score");
					Main.xml.newScore(nameFormatted);
					Main.xml.setScoreInstructions(Main.score.instructions);
					Main.xml.setScoreLoad(Main.score.load);
					Main.textGUI.textArea
					.setText("Saving score "+nameFormatted);
					Main.xml.writeScore();
				}
				else {
					Main.xml.newScore(score);
					Main.xml.setScoreInstructions(Main.score.instructions);
					Main.xml.setScoreLoad(Main.score.load);
					Main.textGUI.textArea
					.setText("Saving score...");
					Main.xml.writeScore();
				}
				searchComplete=true;
			}
		}
		
		if (textLength > 9) {
			if (text.substring(0, 10).equals("save style")) {
				if (textLength>11)
				{
					nameFormatted = text.toLowerCase().substring(11).replace(' ',
							'_');
					Main.xml.newStyle(nameFormatted);
					Main.xml.setStyleArtists(Main.stats.artists);
					Main.textGUI.textArea
					.setText("Saving style "+nameFormatted);
					Main.xml.writeStyle();
				}
				else {
					Main.xml.newStyle(style);
					Main.xml.setStyleArtists(Main.stats.artists);
					Main.textGUI.textArea
					.setText("Saving style..."+style);
					Main.xml.writeStyle();
				}
				searchComplete=true;
			}
		}

		if (textLength > 16&&!searchComplete) {
			if (text.substring(0, 17).equals("save song revised")) {
				System.out.println("SONG NAME: "+song+" ChordRoot "+Main.stats.chordRoots2[0]);
				Main.xml.newSong(song);
				Main.xml
						.setSongChordQualities(Main.stats.chordQualities2);
				Main.xml.setSongChordRoots(Main.stats.chordRoots2);
				Main.xml.setSongDurations(Main.stats.chordDurations2);
				Main.xml.setSongIsMinor(Main.stats.isMinor);
				Main.xml.setSongKey(Main.stats.key);
				Main.xml.setSongMeter(Main.stats.meter);
				Main.xml.setSongMelodyPitch(Main.stats.pitch);
				Main.xml.setSongMelodyOnset(Main.stats.onset);
				Main.xml.writeSongStyle();
				Main.textGUI.textArea.setText("saving song titled "
						+ name + newline);
				searchComplete=true;
			}
		}
		if (textLength > 8&&!searchComplete) {
			if (text.substring(0, 9).equals("save song")) {
				System.out.println("SONG NAME: "+song);
				Main.xml.newSong(song);
				Main.xml
						.setSongChordQualities(Main.stats.chordQualities);
				Main.xml.setSongChordRoots(Main.stats.chordRoots);
				Main.xml.setSongDurations(Main.stats.chordDurations);
				Main.xml.setSongIsMinor(Main.stats.isMinor);
				Main.xml.setSongKey(Main.stats.key);
				Main.xml.setSongMeter(Main.stats.meter);
				Main.xml.setSongMelodyPitch(Main.stats.pitch);
				Main.xml.setSongMelodyOnset(Main.stats.onset);
				Main.xml.writeSongStyle();
				Main.textGUI.textArea.setText("saving song titled "
						+ name + newline);
				searchComplete=true;
			}
		}
		
		
		if (textLength > 10&&!searchComplete) {
			if (text.substring(0, 11).equals("save artist")) {
				int[][] changes = Analyze.toArtistScore();
				Main.xml.newArtist(artist);
				Main.xml.setPitch(Main.stats.pitch);
				Main.xml.setDuration(Main.stats.duration);
				Main.xml.setOnsets(Main.stats.onset);
				Main.xml.setVolume(Main.stats.volume);
				Main.xml.setChordRoots(changes[0]);
				Main.xml.setChordQualities(changes[1]);
				Main.xml.setChordDurations(changes[2]);
				Main.xml.writeArtist();
				Main.textGUI.textArea.setText("saving the style of " + name
						+ newline);
				// Main.textGUI.textArea.setText("!!!! - saving is disabled - !!!! "+name+newline);
			}
		}

		
//		------------ LEARN ---------------------------------------------------


		if (textLength > 11) {
			if (text.substring(0, 11).equals("learn score")) {
				nameFormatted = text.toLowerCase().substring(12).replace(' ',
						'_');
				nameFormatted = nameFormatted.concat("_score");
				name = text.substring(4);
				Main.xml.newScore(nameFormatted);
				score = nameFormatted;
				boolean exists = Main.xml.readScore(nameFormatted);
				if (exists) {
					Main.score.instructions = XML
							.getScoreInstructions(nameFormatted);
					Main.score.load = XML.getScoreLoad(nameFormatted);
					Main.textGUI.textArea
							.setText("I remember " + name
									+ " ...I have already learned this score"
									+ newline);
					newLearn = false;
				} else {
					Main.score.instructions = null;
					Main.textGUI.textArea
							.setText("This one is new to me, learning " + name
									+ newline);
				}
				searchComplete=true;
			}
		}
		
		if (textLength > 10) {
			if (text.substring(0, 11).equals("learn style")) {
				nameFormatted = text.toLowerCase().substring(12)
				.replace(' ', '_');
				nameFormatted = nameFormatted.concat("_style");
				name = text.substring(12);
				Main.xml.newStyle(nameFormatted);
				style = nameFormatted;
				boolean exists = Main.xml.readStyle(nameFormatted);
				if (exists) {
					Main.textGUI.textArea
					.setText(nameFormatted+"...already exists, load?");
				}
				else {
					// Main.stats.init();
					Main.stats.artists=new String[0];
	
					Main.textGUI.textArea
							.setText("This one is new to me, learning a new style called "
									+ name + newline);
				}
				searchComplete=true;
			}
		} 
		
		if (textLength > 9) {
			if (text.substring(0, 10).equals("learn song")) {
				nameFormatted = text.toLowerCase().substring(11)
				.replace(' ', '_');
				nameFormatted = nameFormatted.concat("_song");
				name = text.substring(11);
				Main.xml.newSong(nameFormatted);
				song = nameFormatted;
				boolean exists = Main.xml.readArtist(nameFormatted);
				if (exists) {
					Main.textGUI.textArea
					.setText(nameFormatted+"...already exists, load?");
				}
				else {
					// Main.stats.init();
					Main.stats.init();
	
					Main.textGUI.textArea
							.setText("This one is new to me, learning the style of "
									+ name + newline);
					newLearn = true;
					try {
						BIABReader.analyzeSong();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				searchComplete=true;
			}
		} 
		
		if (textLength > 11) {
			if (text.substring(0, 12).equals("learn melody")) {
				Main.stats.init();
				DumpSequence.run(); 
				searchComplete=true;
			}
		}
		
		if (textLength > 11) {
			if (text.substring(0,12).equals("learn artist")) {
				nameFormatted = text.toLowerCase().substring(13)
						.replace(' ', '_');
				nameFormatted = nameFormatted.concat("_artist");
				Main.xml.newScore(nameFormatted);
				artist = nameFormatted;
				boolean exists = Main.xml.readArtist(nameFormatted);
				if (exists) {
					// Main.stats.count=0;
					// Main.stats.learn=true;
					Main.stats.volume = XML
							.getVolume(nameFormatted);
					Main.stats.pitch = XML.getPitch(nameFormatted);
					Main.stats.duration = XML
							.getDuration(nameFormatted);
					Main.stats.onset = XML.getOnsets(nameFormatted);
					Main.stats.chordRoots = XML
							.getChordRoots(nameFormatted);
					Main.stats.chordQualities = XML
							.getChordQualities(nameFormatted);
					Main.stats.chordDurations = XML
							.getChordDurations(nameFormatted);
					Main.textGUI.textArea.setText("I remember "
							+ name
							+ " ...continuing to learn his style"
							+ newline);
					newLearn = false;
				} else {
					// Main.stats.init();
					Main.stats.init();

					Main.textGUI.textArea
							.setText("This one is new to me, learning the style of "
									+ name + newline);
					newLearn = true;
				}
				searchComplete=true;
				DumpSequence.run();
			}
		}

//		------------ LOAD ---------------------------------------------------

		if (textLength > 10) {
			if (text.substring(0, 10).equals("load style")) {
				searchComplete = true;
				nameFormatted = text.toLowerCase().substring(11).replace(' ',
						'_');
				nameFormatted = nameFormatted.concat("_style");
				name = text.substring(11);
				Main.xml.newStyle(nameFormatted);
				style = nameFormatted;
				boolean exists = Main.xml.readStyle(nameFormatted);
				if (exists) {
					Main.stats.artists = XML
							.getArtists(nameFormatted);
					Main.textGUI.textArea
							.setText("I remember " + nameFormatted
									+ " ...I have already learned this style...loading..."
									+ newline);
					newLearn = false;
				} else {
					Main.textGUI.textArea
							.setText(nameFormatted+"...this one is new to me; starting new style "
									+ name + newline);
				}
				searchComplete=true;
			}
		}
		
		if (textLength > 10) {
			if (text.substring(0, 10).equals("load score")) {
				searchComplete = true;
				nameFormatted = text.toLowerCase().substring(11).replace(' ',
						'_');
				nameFormatted = nameFormatted.concat("_score");
				name = text.substring(11);
				Main.xml.newScore(nameFormatted);
				score = nameFormatted;
				boolean exists = Main.xml.readScore(nameFormatted);
				if (exists) {
					Main.score.instructions = XML
							.getScoreInstructions(nameFormatted);
					Main.score.load = XML.getScoreLoad(nameFormatted);
					Main.stats.artists = Main.score.load[0];
					if (Main.score.load[0] != null) {
						try {
							loadArtists(nameFormatted);
						} catch (JAXBException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					if (Main.score.load[1] != null) {
						try {
							Main.score.load[1][0] = XML.getScoreLoad(nameFormatted)[1][0];
							loadSong(Main.score.load[1][0]);
						} catch (JAXBException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					Main.textGUI.textArea
							.setText("I remember " + nameFormatted
									+ " ...I have already learned this score...loading..."
									+ newline);
					newLearn = false;
				} else {
					Main.textGUI.textArea
							.setText(nameFormatted+"...this one is new to me; starting new score "
									+ name + newline);
				}
				searchComplete=true;
			}
		}
		
		if (textLength > 8&&!searchComplete) {
			if (text.substring(0, 9).equals("load song")) {
				nameFormatted = text.toLowerCase().substring(10)
						.replace(' ', '_');
				nameFormatted = nameFormatted.concat("_song");
				name = text.substring(10);
				Main.xml.newSong(nameFormatted);
				song = nameFormatted;
				boolean exists = Main.xml.readSongStyle(nameFormatted);
				if (exists) {
					Main.score.key = XML.getSongKey(nameFormatted);
					Main.score.isMinor = XML
							.getSongIsMinor(nameFormatted);
					Main.score.changes[0] = XML
							.getSongChordRoots(nameFormatted);
					Main.score.changes[1] = XML
							.getSongChordQualities(nameFormatted);
					Main.score.changes[2] = XML
							.getSongDurations(nameFormatted);
					Main.score.pitch = XML
							.getSongMelodyPitch(nameFormatted);
					Main.score.onset = XML
							.getSongMelodyOnset(nameFormatted);
					Main.textGUI.textArea.setText("I remember " + name
							+ " ...I have already learned this song"
							+ newline);
					newLearn = false;
				} 
				else {
					// Main.stats.init();
					Main.stats.init();

					Main.textGUI.textArea
							.setText("This one is new to me... "
									+ nameFormatted + newline);
				}
				searchComplete=true;
			}
		}
		if (textLength > 10&&!searchComplete) {
			if (text.substring(0, 11).equals("load artist")) {
				// Main.xml.learning=true;
				nameFormatted = text.toLowerCase().substring(12)
						.replace(' ', '_');
				nameFormatted = nameFormatted.concat("_artist");
				name = text.substring(12);
				Main.xml.newArtist(nameFormatted);
				artist = nameFormatted;
				boolean exists = Main.xml.readArtist(nameFormatted);
				if (exists) {
					Main.stats.volume = XML.getVolume(nameFormatted);
					Main.stats.pitch = XML.getPitch(nameFormatted);
					Main.stats.duration = XML
							.getDuration(nameFormatted);
					Main.stats.onset = XML.getOnsets(nameFormatted);
					Main.stats.chordRoots = XML
							.getChordRoots(nameFormatted);
					Main.stats.chordQualities = XML
							.getChordQualities(nameFormatted);
					Main.stats.chordDurations = XML
							.getChordDurations(nameFormatted);
					Main.textGUI.textArea.setText("I remember " + name
							+ " ...continuing to learn his style"
							+ newline);
					newLearn = false;
				} 
				else {
					// Main.stats.init();
					Main.stats.init();

					Main.textGUI.textArea
							.setText("This one is new to me..."
									+ newline);
				}
				searchComplete=true;
			}
		}

		searchComplete = false;
		Main.textGUI.textField.selectAll();
		// Make sure the new text is visible, even if there
		// was a selection in the text area.
		Main.textGUI.textArea.setCaretPosition(Main.textGUI.textArea
				.getDocument().getLength());
	}

	public static int getNum(String content) {
		int value = 0;
		String regex = "([0-9])([0-9])([0-9])";
		Matcher m = Pattern.compile(regex).matcher(content);
		if (m.find()) {
			System.out
					.println("Please enter a strength argument as an integer from 1 to 99");
		} else {
			regex = "([0-9])([0-9])";
			m = Pattern.compile(regex).matcher(content);
			if (m.find()) {
				value = Integer.parseInt(m.group(0));
			} else {
				regex = "([0-9])";
				m = Pattern.compile(regex).matcher(content);
				if (m.find()) {
					value = Integer.parseInt(m.group(0));
				} else {
					System.out
							.println("Please enter a strength argument as an integer from 1 to 99");
				}

			}
		}
		return (value);
	}

	public static void loadSong(String name) throws JAXBException, IOException {
		String nameFormatted = name.toLowerCase().replace(' ', '_');

		Main.xml.newSong(nameFormatted);
		boolean exists = Main.xml.readSongStyle(nameFormatted);
		if (exists) {
			Main.score.key = XML.getSongKey(nameFormatted);
			Main.score.isMinor = XML.getSongIsMinor(nameFormatted);
			Main.score.changes[0] = XML.getSongChordRoots(nameFormatted);
			Main.score.changes[1] = XML.getSongChordQualities(nameFormatted);
			Main.score.changes[2] = XML.getSongDurations(nameFormatted);
			Main.score.pitch = XML.getSongMelodyPitch(nameFormatted);
			Main.score.onset = XML.getSongMelodyOnset(nameFormatted);
			Main.textGUI.textArea.setText("I remember " + name
					+ " ...I have already learned this song");
		} else {
			// Main.stats.init();
			Main.stats.init();

			Main.textGUI.textArea.setText("This one is new to me... "
					+ nameFormatted);
		}
	}

	public static void loadArtists(String name) throws JAXBException,
			IOException {
		// Main.xml.learning=true;
		String nameFormatted = name.toLowerCase().replace(' ', '_');

		Main.xml.newArtist(nameFormatted);
		boolean exists = Main.xml.readArtist(nameFormatted);
		if (exists) {
			Main.stats.artists = XML.getArtists(nameFormatted);
			Main.score.load[0] = Main.stats.artists;
			Main.textGUI.textArea.setText("I remember " + name
					+ " ...continuing to load this style");
		} else {
			// Main.stats.init();
			Main.stats.init();

			Main.textGUI.textArea.setText("This one is new to me...");
		}
	}
	
	public static void printInstructions() {
		if (Main.score.instructions != null) {
			System.out.println("Instructions: ");
			for (int i = 0; i < Main.score.instructions.length; i++) {
				System.out.println(Main.score.instructions[i][0] + " "
						+ Main.score.instructions[i][1] + " "
						+ Main.score.instructions[i][2]);
			}
		}
	}
	
	public static void load(String nameFormatted) throws JAXBException, IOException {
		String text = nameFormatted;
		nameFormatted = text.replace(' ', '_');
		nameFormatted = nameFormatted.concat("_score");
		Main.xml.newScore(nameFormatted);
		score = nameFormatted;
		boolean exists = Main.xml.readScore(nameFormatted);
		if (exists) {
			Main.score.instructions = XML
					.getScoreInstructions(nameFormatted);
			Main.score.load = XML.getScoreLoad(nameFormatted);
			Main.stats.artists = Main.score.load[0];
			if (Main.score.load[0] != null) {
				try {
					loadArtists(nameFormatted);
				} catch (JAXBException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (Main.score.load[1] != null) {
				try {
					Main.score.load[1][0] = XML.getScoreLoad(nameFormatted)[1][0];
					loadSong(Main.score.load[1][0]);
				} catch (JAXBException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			Main.textGUI.textArea
					.setText("I remember " + nameFormatted
							+ " ...I have already learned this score...loading..."
							+ newline);
			newLearn = false;
		} else {
			Main.textGUI.textArea
					.setText(nameFormatted+"...this one is new to me; starting new score "
							+ name + newline);
		}
	}
}
